package com.company.dcm.server.tcp;

import com.company.dcm.common.SpringBeanUtil;
import com.company.dcm.server.protocol.ProtocolMessage;
import com.company.dcm.server.protocol.ProtocolMessageEncoder;
import com.company.dcm.server.protocol.ProtocolTask;
import com.company.dcm.server.protocol.enums.ProtocolMessageTypeEnum;
import com.company.dcm.server.protocol.enums.ProtocolTaskOptionEnum;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;

import java.io.IOException;
import java.util.List;

public class MessageSender {
    public static void sendMessage(String equipment_no, byte[] bytes) {
        String ipPort = ConnectionManager.serialToIpPort.get(equipment_no);
        if(ipPort == null) {
            throw new RuntimeException("设备从未发送过设备号或连接已断开");
        }
        Channel channel = ConnectionManager.getChannel(ipPort);
        if (channel != null) {
            ByteBuf data = Unpooled.wrappedBuffer(bytes);
            channel.writeAndFlush(data);
        } else {
            System.out.println("Channel not found for client: " + ipPort);
            throw new RuntimeException("Channel not found for ipPort: " + ipPort);
        }
    }

    public static void sendMessageByIP(String ip, byte[] bytes) {
        String ipPort = ConnectionManager.serialToIpPort.get(ip);//通过前面以ip为键获得ota的ipPort

        if(ip == null) {
            throw new RuntimeException("设备从未发送过设备号或连接已断开");
        }
        Channel channel = ConnectionManager.getChannel(ipPort);
        if (channel != null) {
            ByteBuf data = Unpooled.wrappedBuffer(bytes);
            channel.writeAndFlush(data);
        } else {
            System.out.println("Channel not found for client: " + ipPort);
            throw new RuntimeException("Channel not found for ipPort: " + ipPort);
        }
    }

    public static byte[] handleMessage(ProtocolMessageTypeEnum messageType, ProtocolTaskOptionEnum option) throws IOException {
        TCPService tcpService = SpringBeanUtil.getBean("TCPService");
        byte[] response = new byte[]{0};
        boolean hasMessage = false;
        ProtocolMessageEncoder encoder;
        if (messageType == ProtocolMessageTypeEnum.INFORMATION) {
            encoder = new ProtocolMessageEncoder((byte) 1);
            switch (option) {
                case GET_MES_CAMERA: {
                    encoder.encode(tcpService.getMESCamera());
                    hasMessage = true;
                    break;
                }
            }
        }
        else if (messageType == ProtocolMessageTypeEnum.BIG_DATA){
            encoder = new ProtocolMessageEncoder((byte) 2);
        }
        else if (messageType == ProtocolMessageTypeEnum.OTA){
            encoder = new ProtocolMessageEncoder((byte) 3);
        }
        else {
            encoder = new ProtocolMessageEncoder((byte) 4);
            if (messageType == ProtocolMessageTypeEnum.DATA_PASSTHROUGH_PACKET) {
                switch (option) {
                    case GET_MES_SWITCH_DETECTION: {
                        encoder.encode(tcpService.getMESSwitchDetection());
                        hasMessage = true;
                        break;
                    }
                    case GET_MES_IMAGE_REPORT: {
                        encoder.encode(tcpService.getMESImageReport());
                        hasMessage = true;
                        break;
                    }
                }
            }
        }


        if (hasMessage) {
            return encoder.getMessage();
        }
        return response;
    }

    public static byte[] handleMessage(ProtocolMessageTypeEnum messageType, ProtocolTaskOptionEnum option,String SoftwareVersion,String HardwareVersion) throws IOException {
        TCPService tcpService = SpringBeanUtil.getBean("TCPService");
        byte[] response = new byte[]{0};
        boolean hasMessage = false;
        ProtocolMessageEncoder encoder;
        if (messageType == ProtocolMessageTypeEnum.INFORMATION) {
            encoder = new ProtocolMessageEncoder((byte) 1);
            switch (option) {
                case NOTICE_OTA_UPDATED:{
                    encoder.encode(tcpService.noticeOtaUpdate(SoftwareVersion,HardwareVersion));
                    hasMessage = true;
                    break;
                }
                case GET_MES_CAMERA: {
                    encoder.encode(tcpService.getMESCamera());
                    hasMessage = true;
                    break;
                }
            }
        }
        else if (messageType == ProtocolMessageTypeEnum.BIG_DATA){
            encoder = new ProtocolMessageEncoder((byte) 2);
        }
        else if (messageType == ProtocolMessageTypeEnum.OTA){
            encoder = new ProtocolMessageEncoder((byte) 3);
        }
        else {
            encoder = new ProtocolMessageEncoder((byte) 4);
            if (messageType == ProtocolMessageTypeEnum.DATA_PASSTHROUGH_PACKET) {
                switch (option) {
                    case GET_MES_SWITCH_DETECTION: {
                        encoder.encode(tcpService.getMESSwitchDetection());
                        hasMessage = true;
                        break;
                    }
                    case GET_MES_IMAGE_REPORT: {
                        encoder.encode(tcpService.getMESImageReport());
                        hasMessage = true;
                        break;
                    }
                }
            }
        }


        if (hasMessage) {
            return encoder.getMessage();
        }
        return response;
    }

}
